package org.x.casemodule.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.x.casemodule.dto.*;
import org.x.casemodule.service.ScheduleService;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/schedule")
public class ScheduleController {

    @Resource
    private ScheduleService scheduleService;

    @PostMapping("/create")
    public Map<String, Object> createSchedule(@RequestBody @Valid ScheduleCreateRequest request) {
        log.info("创建日程请求: {}", request);

        ScheduleCreateResponse response = scheduleService.createSchedule(request);

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", response);

        return result;
    }


    @PostMapping("/update")
    public Map<String, Object> updateSchedule(@RequestBody @Valid ScheduleUpdateRequest request) {
        log.info("更新日程请求: {}", request);

        ScheduleCreateResponse response = scheduleService.updateSchedule(request);

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", response);

        return result;
    }



    @DeleteMapping("/delete/{eventId}")
    public Map<String, Object> deleteSchedule(@PathVariable Long eventId) {
        log.info("删除日程请求: eventId={}", eventId);

        scheduleService.deleteSchedule(eventId);

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "删除成功");

        return result;
    }



    // ScheduleController.java
    @PostMapping("/view")
    public Map<String, Object> getView(@RequestParam String view,
                                       @RequestParam(required = false) String date,
                                       @RequestParam(required = false, defaultValue = "false") Boolean includeShared) {
        log.info("查询日程视图请求: view={}, date={}, includeShared={}", view, date, includeShared);

        // 调用服务层处理逻辑
        Map<String, Object> result = scheduleService.getView(view, date, includeShared);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("data", result);

        return response;
    }




    // ScheduleController.java
    @PostMapping("/court")
    public Map<String, Object> shareCourtSchedule(@RequestBody @Valid CourtScheduleRequest request) {
        log.info("共享法院日程请求: {}", request);

        // 调用服务层处理法院日程共享逻辑
        CourtScheduleResponse response = scheduleService.shareCourtSchedule(request);

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", response);

        return result;
    }



    // 在 ScheduleController.java 中添加以下方法
    @PostMapping("/share")
    public Map<String, Object> shareSchedule(@RequestBody @Valid ScheduleShareRequest request) {
        log.info("共享日程请求: {}", request);

        // 调用服务层处理日程共享逻辑
        ScheduleShareResponse response = scheduleService.shareSchedule(request);

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", response);

        return result;
    }



    // org/x/casemodule/controller/ScheduleController.java (补充方法)
    @PostMapping("/sync")
    public ResponseEntity<ScheduleSyncResponse> syncExternalCalendar(
            @RequestBody @Valid ScheduleSyncRequest request,
            HttpServletRequest httpRequest) {

        try {
            log.info("开始外部日历同步: platform={}, direction={}",
                    request.getPlatform(), request.getSyncDirection());

            ScheduleSyncResponse response = scheduleService.syncExternalCalendar(request);

            log.info("外部日历同步完成: sessionId={}, stats={}",
                    response.getSyncSessionId(), response.getStats());

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("外部日历同步失败: {}", e.getMessage(), e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(new ScheduleSyncResponse());
        }
    }





}



